import { reactive } from 'vue';
import _ from 'lodash';
import * as Store from '@/store';
import useLeft from './useLeft';
import useContent from './useContent';
import useRight from './useRight';
import { randomString } from "@/utils/utils";
import type * as ApiType from "@/api/filmRead/filmRead.d";
import type * as FilmReadStoreType from "@/store/modules/filmRead/type";
const useToolBarUtils = () => {
  const FilmRead = Store.useFilmRead();
  const interfaceData = FilmRead.interfaceData
  const dictionaryData = FilmRead.dictionaryData
  const LeftHooks = useLeft();
  const ContentHooks = useContent();
  const RightHooks = useRight();
  // 左侧病灶列表增删改事件
  /**
   * 病灶增加数据的通用方法
   * @param createObj 当前病灶列表要追加的数据集合
   */
  const lesionAdd = (createObj: any) => {
    let addLesionObj = {} as {[x: string | number]: any};
    let addSeriesArr = {} as FilmReadStoreType.Component[];
    let addImageDataObj = {} as FilmReadStoreType.LesionItem;
    if(dictionaryData.lesionDataList) {
      const { color, eyeStatus, id, index, lesion, tool, result, layerStart, layerEnd, groupId } = createObj;
      addLesionObj = _.cloneDeep({
        id,
        code: lesion,
        color: color ?? "",
        eyeStatus: true,
        index: Object.values(dictionaryData.lesionDataList).length - 1,
        lesion: dictionaryData.lesionList[lesion].name,
        tool,
        groupId,
        alg: false
      })
      let compList = dictionaryData.toolList[tool].find(item => item.lesion === addLesionObj.code);
      addSeriesArr = (compList && compList.imageCompList as FilmReadStoreType.Component[]) ?? ([] as ApiType.ImageCompItem[] & FilmReadStoreType.Component[])
      addSeriesArr = addSeriesArr.map(item =>({...item, id: -1, lesionId:addLesionObj.id}))
      const key = randomString();
      addImageDataObj = _.cloneDeep({
        id,
        index: Object.values(dictionaryData.lesionDataList).length,
        resultType: tool === "FREEHAND_DAUBER" ? 1 : 2,
        result,
        resultDetailMap: null
      }) as FilmReadStoreType.LesionItem

      // 增加病灶
      LeftHooks.lesionAdd(addLesionObj);
      // 聚焦病灶
      LeftHooks.lesionSetActive(addLesionObj.id.toString());
      // 增加征象
      RightHooks.seriesDataAdd(addSeriesArr, id);
      // 切换征象
      seriesChangeActive(addLesionObj.id);
      // 增加图像
      ContentHooks.imageDataAdd(addImageDataObj);
    }
  }

  /**
   * 帧段增加数据的通用方法
   * @param createObj 创建帧段数据集合
   */
  const frameAdd = (createObj: {id: number, info: {startFrame: number, endFrame: number}, type: string, taskId: number, arId: number}) => {
    let addFrameObj = {} as {[x: string | number]: any}
    if (dictionaryData.frameList) {
      const { id, info, type, taskId, arId } = createObj;
      addFrameObj = _.cloneDeep({
        ...createObj,
        createTime: Date.now(),
        updateTime: Date.now(),
        eyeStatus: true,
        algProcessed: false,
        visibile: false
      })
    }
    // 增加帧段
    LeftHooks.frameAdd(addFrameObj)
  }

  /**
   * 通用改变病灶数据事件
   * @param lesionId 病灶id
   * @param changeObj 改变数据对象
   */
  const lesionUpdate = (lesionId: number, changeObj: any) => {
    if(dictionaryData.lesionDataList) {
      const lesionData = dictionaryData.lesionDataList[lesionId];
      // 改变相应的值
      for(let key in changeObj){
        lesionData[key] && (lesionData[key] = changeObj[key])
      }
    }
    ContentHooks.imageDataUpdate(lesionId, changeObj)
  }

  /**
   * 超声视频病灶数据更新方法
   * @param lesionId 病灶id
   * @param updateObj 改变数据对象
   */
  const videoLesionUpdate = (lesionId: number, updateObj: any) => {
    ContentHooks.imageDataUpdate(lesionId, updateObj);
  }

  /**
   * 通用删除病灶数据事件
   * @param lesionId 当前病灶id
   * @param frameId 帧段id
   */
  const lesionDel = (lesionId: number, frameId?: number) => {
    // if (interfaceData.pageSettingList.imageLayout === "VIDEO_ANNO" && type === 'multi') {
    //   if (LeftHooks.leftHooksData.currentIndex !== "") {
    //     lesionSetActive("")
    //   }
    // } else {
    //   (lesionId.toString() === LeftHooks.leftHooksData.currentIndex) && lesionSetActive(lesionId.toString())
    // }
    (lesionId.toString() === LeftHooks.leftHooksData.currentIndex) && lesionSetActive(lesionId.toString());
    LeftHooks.lesionDel(lesionId, frameId);
    RightHooks.seriesDataDel(lesionId);
    ContentHooks.imageDataDel(lesionId);
  }

  /**
   * 通用删除帧段数据事件
   * @param frameId 当前帧段id
   */
  const frameDel = (frameId: number) => {
    if (interfaceData.pageSettingList.imageLayout === "VIDEO_ANNO" && LeftHooks.leftHooksData.currentIndex !== "") {
      lesionSetActive("")
    }
    ContentHooks.imageFrameDel(frameId)
    RightHooks.seriesFrameDel(frameId)
    // 删除病灶必须放最后否则会导致征象和图像数据集合不能正常删除
    LeftHooks.frameDel(frameId)
  }

  /**
   * 更新帧段方法
   * @param updateObj 帧段更新数据
   */
  const frameUpdate = (updateObj: {id: number, info: { startFrame: number; endFrame: number; } }) => {
    LeftHooks.frameUpdate(updateObj)
  }
  
  /**
   * 通用聚焦当前病灶方法
   * @param lesionId 当前病灶id
   * @param flag 是否可反选，true-一直选中，false||不传-当前选中一致会反选
   */
  const lesionSetActive = (lesionId: string, flag?: boolean) => {
    if(flag){
      LeftHooks.setLesionActive(lesionId)
    }else{
      LeftHooks.lesionSetActive(lesionId);
    }
    seriesChangeActive(+lesionId);
  }

  /**
   * 通用移除聚焦病灶方法
   */
  const lesionRemoveActive = () => {
    LeftHooks.lesionRemoveActive();
  }

  // 右侧征象聚焦事件
  const seriesChangeActive = (lesionId?: number) => {
    RightHooks.seriesChangeActive(lesionId);
  }

  /**
   * 通用聚焦病灶方法
   * @param lesionId 当前病灶id
   */
  const contentActive = (lesionId: number) => {
    ContentHooks.setLesionActive(lesionId);
  }

  /**
   * 通用聚焦帧段方法
   * @param frameId 当前帧段组id
   */
  const frameActive = (frameId: number) => {
    LeftHooks.frameActive(frameId)
  }

  return {
    lesionEvent: reactive({
      lesionAdd,
      lesionUpdate,
      lesionDel,
      lesionSetActive,
      lesionRemoveActive,
      contentActive,
      frameDel,
      frameAdd,
      frameUpdate,
      videoLesionUpdate,
      frameActive
    }),
    seriesEvent: reactive({
      seriesChangeActive
    })
  }
};

export default useToolBarUtils;